package com.approval.approvalapp.index.cygn;

import android.app.DatePickerDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.HyssyjlxqListBean;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.google.gson.JsonObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HysSyjlxqListActivity extends BaseActivity {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.empty_ll)
    LinearLayout emptyLl;
    @Bind(R.id.time_tv)
    TextView timeTv;
    @Bind(R.id.type_ll)
    LinearLayout typeLl;
    @Bind(R.id.rv)
    RecyclerView rv;
    private String implacen;
    private long time;
    private Adapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_hys_syjlxq_list;
    }

    @Override
    public void initView() {
        implacen = getIntent().getStringExtra("implacen");

        titleTv1.setText(implacen + "的使用记录");
    }

    @Override
    public void initData() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd");
        String format = simpleDateFormat.format(new Date(System.currentTimeMillis()));
        timeTv.setText(format);
        time =   System.currentTimeMillis();


        rv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new Adapter();
        rv.setAdapter(adapter);

        refresh();

    }

    @OnClick({R.id.back_iv, R.id.type_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.type_ll:
                Calendar ca = Calendar.getInstance();
                int mYear = ca.get(Calendar.YEAR);
                int mMonth = ca.get(Calendar.MONTH);
                int mDay = ca.get(Calendar.DAY_OF_MONTH);


                DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                Log.d("time", "year:" + year);
                                Log.d("time", "month:" + (month + 1));
                                Log.d("time", "dayOfMonth:" + dayOfMonth);

                                try {
                                    Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(year
                                            + "-" + (month + 1) + "-" + dayOfMonth);
                                    time = parse.getTime();
                                    Log.d("time", "time:" + time);

                                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd");
                                    String format = simpleDateFormat.format(new Date(time));
                                    timeTv.setText(format);
                                    Log.d("time", format);

                                    // 刷新列表
                                    refresh();
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                            }
                        },
                        mYear, mMonth, mDay);
                DatePicker datePicker = datePickerDialog.getDatePicker();
                datePicker.setMaxDate(new Date().getTime());//设置日期的上限日期
                datePickerDialog.show();
                break;
        }
    }

    public void refresh() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("areacode" , SpUtil.getString(this,SpUtil.REGION_CODE,""));
        jsonObject.put("implacen" , implacen);
        jsonObject.put("select_time" , (time/1000));
        NetRequsetUtil.getInstance().netRequestPostJson("v1/center/user_hui_list_record", jsonObject.toString(), new NetDownResponse() {
            @Override
            public void success(String str) {
                List<HyssyjlxqListBean> hyssyjlxqListBeans = JSON.parseArray(str, HyssyjlxqListBean.class);
                if (hyssyjlxqListBeans == null || hyssyjlxqListBeans.size() == 0) {
                    emptyLl.setVisibility(View.VISIBLE);
                } else {
                    emptyLl.setVisibility(View.GONE);
                    adapter.setHyssyjlxqListBeans(hyssyjlxqListBeans);
                }
            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
            }

            @Override
            public void erro() {

            }

            @Override
            public void finish() {

            }
        });

    }

    class Adapter extends RecyclerView.Adapter<Adapter.Holder> {
        List<HyssyjlxqListBean> hyssyjlxqListBeans;

        public void setHyssyjlxqListBeans(List<HyssyjlxqListBean> hyssyjlxqListBeans) {
            this.hyssyjlxqListBeans = hyssyjlxqListBeans;
            notifyDataSetChanged();
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_syjlxq, parent, false);
            Holder holder = new Holder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            holder.title.setText(hyssyjlxqListBeans.get(position).getImTitle());
            holder.tv1.setText("申请人: " + hyssyjlxqListBeans.get(position).getXNAME());
            holder.tv2.setText("时间段: " + hyssyjlxqListBeans.get(position).getImStartTime() + "-"
            + hyssyjlxqListBeans.get(position).getImEndTime());

        }

        @Override
        public int getItemCount() {
            if (hyssyjlxqListBeans == null) {
                return 0;
            } else {
                return hyssyjlxqListBeans.size();
            }
        }

        class Holder extends RecyclerView.ViewHolder {
            @Bind(R.id.data_article)
            ImageView dataArticle;
            @Bind(R.id.tv1)
            TextView tv1;
            @Bind(R.id.tv2)
            TextView tv2;
            @Bind(R.id.title)
            TextView title;
            public Holder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);

            }
        }

    }
}
